<?php

class travideSearchindexrebuildTask extends sfBaseTask {


    protected function configure() {
        // // add your own arguments here
        // $this->addArguments(array(
        //   new sfCommandArgument('my_arg', sfCommandArgument::REQUIRED, 'My argument'),
        // ));

        $this->addOptions(array(
                new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name','frontend'),
                new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
                new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
                new sfCommandOption('start-uin', null, sfCommandOption::PARAMETER_OPTIONAL, 'Start range of UINs to index', null),
                new sfCommandOption('end-uin', null, sfCommandOption::PARAMETER_OPTIONAL, 'End range of UINs to index', null),
                // add your own options here
        ));

        $this->namespace        = 'travide';
        $this->name             = 'search-index-rebuild';
        $this->briefDescription = 'rebuilds the search index for Collection Items in the database';
        $this->detailedDescription = <<<EOF
The [travide:search-index-rebuild|INFO] task does things.
Call it with:

  [php symfony travide:search-index-rebuild|INFO]
EOF;
    }

    protected function execute($arguments = array(), $options = array()) {
        // initialize the database connection
        $databaseManager = new sfDatabaseManager($this->configuration);
        $connection = $databaseManager->getDatabase($options['connection'])->getConnection();

        // add your code here
        $this->logSection('rebuild','Retrieving Collection Items');
        $collectionItemQuery = Doctrine::getTable('CollectionItem')->addNonArchivedQuery();
        if ($options['start-uin']) {
            $collectionItemQuery = Doctrine::getTable('CollectionItem')->addUinStartRangeQuery($options['start-uin'],$collectionItemQuery);
        }

        if ($options['end-uin']) {
            $collectionItemQuery = Doctrine::getTable('CollectionItem')->addUinEndRangeQuery($options['end-uin'],$collectionItemQuery);
        }
        $collectionItems = $collectionItemQuery->execute();

        $itemCount = $collectionItems->count();

        $this->logSection('rebuild','Processing '.$itemCount.' Collection Items');

        $counter = 0;
        foreach($collectionItems as $item) {
            $item->updateSearchIndex();
            $counter += 1;
            if (($counter % 10) == 0) {
                $this->logSection('rebuild', $counter . ' items completed');
            }
        }
        $this->logSection('rebuild','Complete: Processed '.$counter.' Collection Items');
        $this->logSection('Optimization', 'Requesting Search Index Optimization');
        Doctrine::getTable('CollectionItem')->optimizeLuceneIndex();
        $this->logSection('Optimization', 'Search Index Optimization completed');
    }
}


